package cn.ac.iscas.service.transformer;

import cn.ac.iscas.base.utils.ParameterUtils;
import cn.ac.iscas.base.utils.SpringUtil;
import cn.ac.iscas.domain.source.FlinkSourceColumn;
import cn.ac.iscas.service.queue.flink.FlinkTableQueueService;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author LJian
 * @version 1.0
 * @description: TODO
 * @date 2022/7/19 13:35
 */
public class FilterConditionTransformer {
    private final static String ID = "filter_condition";
    private final static String RULE_KEY = "rule";
    private final static String VALUE_KEY = "value";
    private final static String COLUMN_KEY = "column";
    private String jobId;
    private String componentId;
    private String frontTableName;
    private List<FlinkSourceColumn> frontColumns;

    private String currentTableName;
    private List<FlinkSourceColumn> currentColumns;

    public FilterConditionTransformer(String jobId, String taskId) {
        this.jobId = jobId;
        this.componentId = taskId;
    }

    public FilterConditionTransformer(String jobId, String taskId, String frontTableName, List<FlinkSourceColumn> frontColumns) {
        this.jobId = jobId;
        this.componentId = taskId;
        this.frontTableName = frontTableName;
        this.frontColumns = frontColumns;
        this.currentColumns = frontColumns;
    }


    public FilterConditionTransformer() {

    }

    public String getSql(Map<String, Object> parameters) {
        this.currentTableName = String.format("%s.%s.%s_%s", jobId, jobId, ID, UUID.randomUUID().toString().substring(0,6));
        String createViewSql = String.format("CREATE VIEW %s AS ", this.currentTableName);
        String rule = ParameterUtils.getRequiredString(parameters, RULE_KEY);
        String value = ParameterUtils.getRequiredString(parameters, VALUE_KEY);
        String column = ParameterUtils.getRequiredString(parameters, COLUMN_KEY);
        String selectSql = "";
        switch (rule.toLowerCase(Locale.ROOT)) {
            case "greater_equal":
                selectSql = String.format("( SELECT * FROM %s WHERE %s >= %s )", frontTableName, column, value);
                break;
            case "greater":
                selectSql = String.format("( SELECT * FROM %s WHERE %s > %s )", frontTableName, column, value);
                break;
            case "equal":
                selectSql = String.format("( SELECT * FROM %s WHERE %s = %s )", frontTableName, column, value);
                break;
            case "less":
                selectSql = String.format("( SELECT * FROM %s WHERE %s < %s )", frontTableName, column, value);
                break;
            case "less_equal":
                selectSql = String.format("( SELECT * FROM %s WHERE %s <= %s )", frontTableName, column, value);
                break;
            default:
                break;
        }
        updateSource();

        return createViewSql + selectSql;
    }

    public void updateSource() {
        FlinkTableQueueService flinkTableQueueService = SpringUtil.getBean(FlinkTableQueueService.class);
        Map<String, List<FlinkSourceColumn>> tableColumns = new ConcurrentHashMap<>(8);
        tableColumns.put(this.currentTableName, this.currentColumns);
        flinkTableQueueService.put(this.jobId, this.componentId, tableColumns);
    }
}
